<?php

namespace App\Http\Controllers\Backend;

use App\User;
use Validator;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

class AuthController extends Controller {
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    /**
     * Where to redirect users after login / registration.
     *
     * @var string
     */
    protected $redirectTo = 'dashboard';

    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        // $this->middleware($this->guestMiddleware(), ['except' => 'logout']);
    }

    public function getLogin()
    {

        return view('backend.login');
    }

    public function getLogout()
    {
        \Auth::logout();

        return redirect('admin/login')->withInfo('您已经安全退出登陆');
    }

    /**
     * Handle an authentication attempt.
     *
     * @return Response
     */
    public function authenticate(Request $request)
    {

        $email = $request->input('username');
        $password = $request->input('password');
        $remember = $request->input('remember', 0);

        if (\Auth::attempt(['email' => $email, 'password' => $password], $remember)) {
            // Authentication passed...
            return redirect()->intended('admin/dashboard');
        } else {
            return redirect('admin/login')
                    ->withInput($request->except('password'))
                    ->withError('账号或者密码不正确');
        }
    }
}
